import utils from "./utils";
const socket = new WebSocket("ws://localhost:5555");
import * as db from "./db";
import command from "./command";
import { priceJob } from "./job-price";
import { regouJob } from "./job-regou";
import { binanceJob } from "./job-binance";
import { caidan2Job } from "./job-caidan2";
import axios from "axios";

let botid: string = "";
socket.addEventListener("message", async (messageEvent: MessageEvent) => {
  const event: { data: string } = JSON.parse(messageEvent.data);
  const data: { content?: string; wxid?: string } = JSON.parse(event.data);
  console.log(data);
  if (data.content && data.wxid && data.type == 1) {
    try {
      await command(data.content, data.wxid, botid, data.sender);
    } catch (e) {
      console.log(e);
    }
  }

  if (data.wxid && data.wxid.indexOf("chatroom") < 0) {
    let sended = await db.getSettings(data.wxid, "welcome");
    if (!sended) {
      await utils.sendMessage(
        data.wxid,
        `
机器人功能：
查询功能: 免费
打满推送: 20U/年
推特监听：20U/年
支持微信 QQ TG
封群可免费换
两个群以上享8折`,
      );
      await db.saveSettings(data.wxid, "welcome", true);
    }
  }

  // if (data.type == 1) {
  //   let message = {
  //     wid: botid,
  //     text: data.content,
  //     toId: botid,
  //     fromId: data.wxid.indexOf("@") > 0 ? data.sender : data.wxid,
  //     rid: data.wxid.indexOf("@") > 0 ? data.wxid : "",
  //     type: "7",
  //     fromName: "",
  //     date: new Date(),
  //   };
  //   try {
  //     axios
  //       .post("https://pug.minapp.xin/api/messages", {
  //         messages: [message],
  //       })
  //       .then()
  //       .catch(console.error);
  //   } catch (err) {
  //     console.log(err);
  //   }
  // }

  if (data.type == 10000) {
    console.log(data);
    if (data.content?.includes("当前群涉及违规")) {
      let res = await fetch(
        "https://four-api.minapp.xin/api/four/rooms/" + botid,
        {
          method: "POST",
          body: JSON.stringify({
            roomid: data.wxid,
            enable: false,
          }),
        },
      );
      await res.json();
    }
  }
});

socket.addEventListener("open", async (event: Event) => {
  const response = await utils.getMyself();
  console.log(response);
  const { wxid } = response;
  botid = wxid;
  console.log("connected");
});

socket.addEventListener("close", (event: CloseEvent) => {
  console.log("close");
});

socket.addEventListener("error", (error: Event) => {
  console.log(error);
});

// socket.o((s,e)=>{console.error(e)});

await utils.sendMessage(
  "filehelper",
  `机器人启动成功
指令集
1.查看设置
2.币价关注 + 币名
3.币价取关 + 币名
4.币安新币 + 开/关
5.热狗推送 + 开/关

`,
);

// setInterval(async function () {
//   try {
//     await priceJob();
//   } catch (err: unknown) {
//     console.log(err);
//   }
// }, 1000);

// setInterval(async function () {
//   try {
//     await regouJob();
//   } catch (err: unknown) {
//     console.log(err);
//   }
// }, 1000);

setInterval(async function () {
  try {
    await caidan2Job(botid);
  } catch (err: unknown) {
    console.log(err);
  }
}, 500);

// setInterval(async function () {
//   try {
//     await binanceJob();
//   } catch (err: unknown) {
//     console.log(err);
//   }
// }, 60000);

process.on("uncaughtException", (err) => {
  console.error("未捕获全局异常", err);
});
